package com.lagou.controller;

import com.lagou.domain.PromotionAdVO;
import com.lagou.domain.ResponseResult;
import com.lagou.service.PromotionAdService;
import com.lagou.utils.UploadHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/promotionAd")
public class PromotionAdController {
    @Autowired
    private PromotionAdService promotionAdService;

    @RequestMapping("/findPromotionAdByPage")
    public ResponseResult findPromotionAdByPage(PromotionAdVO promotionAdVO) {
        return new ResponseResult(true, 200, "success", promotionAdService.findAllPromotionAdByPage(promotionAdVO));
    }

    @RequestMapping("/promotionAdUpload")
    public ResponseResult fileUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        String fileName = UploadHelper.upload(file, request);
        Map<String, String> map = new HashMap<>();
        map.put("fileName", fileName);
        map.put("filePath", request.getHeader("Host") + "/upload/" + fileName);
        return new ResponseResult(true, 200, "success", map);
    }

    @RequestMapping("/updatePromotionAdStatus")
    public ResponseResult updatePromotionAdStatus(Integer id, Integer status) {
        promotionAdService.updatePromotionAdStatus(id, status);
        return new ResponseResult(true, 200, "success", null);
    }
}
